<template>
  <div>
    <el-card class="login-form-layout">
      <el-form
        autocomplete="on"
        :model="loginForm"
        ref="loginForm"
        label-position="left"
      >
        <div style="text-align: center">
          <svg-icon icon-class="login-mall" style="width: 56px;height: 56px;color: #409EFF"></svg-icon>
        </div>
        <h2 class="login-title color-main">注册账号</h2>
        <el-form-item prop="username" label="">
          用户名：
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autocomplete="on"
            placeholder="请输入用户名"
          >
            <!-- <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span> -->
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="">
          密码：
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autocomplete="on"
            placeholder="请输入密码"
          >
            <!-- <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span> -->
            <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="">
          确认密码：
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password1"
            autocomplete="on"
            placeholder="请输入密码"
          >
            <!-- <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span> -->
            <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px">
          <el-button
            style="width: 48%"
            type="primary"
            :loading="loading"
            @click.native.prevent="handleLogin"
          >用户注册</el-button>
          <el-button
            style="width: 48%"
            type="primary"
            :loading="loading"
            @click="gotoLogin"
          >返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
 
<script>
export default {
  name: "register",
  data() {
    return {
      loginForm: {
        username: null,
        password: null,
        password1: null
      },
      loading: false,
      pwdType: "password",
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    gotoLogin(){
      this.$router.push('/')
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          //调用store下的Login方法
          this.$store
            .dispatch("Register", this.loginForm)//读取loginForm里面的username和password
            .then(response => {//声明response变量，类型是json
              this.loading = false;
              let code = response.data.code;//后端传给前端的值为data，code，message，传值给code
              if (code == 200) {
                this.$router.push({
                  path: "/success",
                  query: { data: response.data.data }
                });
              } //this.$router.push根据路由将query里面的信息push给/success
              else {
                this.$router.push({
                  path: "/error",
                  query: { message: response.data.message }
                });
              }//this.$router.push根据路由将query里面的信息push给/error
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          // eslint-disable-next-line no-console
          console.log("参数验证不合法！");
          return false;
        }
      });
    }
  }
};
</script>
 
<style scoped>
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #409eff;
}
 
.login-title {
  text-align: center;
}
 
.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>